/**
 * Wihidum - Efficient Broker Hierarchy for Publish Subscribe Middleware
 *
 * License      :   Apache License 2.0
 * Home Page    :   http://code.google.com/p/wihidum/
 * Mailing List :   wihidum@googlegroups.com
 */

package lk.mrt.cse.wihidum.listener.storage;

import org.apache.log4j.*;
import org.hibernate.*;
import org.hibernate.criterion.*;

import java.util.*;

/**
 * Access data about publications in database
 */
public class DeliveryElementDAO
        implements DeliveryElementManager
{

    Session session = null;
    private static final Logger logger = Logger.getLogger (DeliveryElementDAO.class);

    /**
     * Default constructor
     */
    public DeliveryElementDAO ()
    {
        this.session = DataBaseManager.getSessionFactory ().getCurrentSession ();
    }

    /**
     * @see lk.mrt.cse.wihidum.listener.storage.DeliveryElementManager#saveDeliveryElement(DeliveryElement)
     */
    public boolean saveDeliveryElement (DeliveryElement deliveryElement)
    {
        try
        {
            Transaction tx = session.beginTransaction ();
            session.save (deliveryElement);
            tx.commit ();
            logger.info ("Delivery element saved successfully");
            return true;
        }
        catch (HibernateException ex)
        {
            logger.error ("Error in saving delivery element", ex);
            return false;
        }
    }

    /**
     * @see lk.mrt.cse.wihidum.listener.storage.DeliveryElementManager#getDeliveryElements(String)
     */
    public ArrayList<DeliveryElement> getDeliveryElements (String subscriptionID)
    {
        try
        {
            ArrayList<DeliveryElement> searchedList;

            session.beginTransaction ();
            Criteria criteria = session.createCriteria (DeliveryElement.class)
                    .add (Restrictions.eq ("subscriptionID", subscriptionID));
            searchedList = (ArrayList<DeliveryElement>) criteria.list ();

            if (searchedList == null || searchedList.isEmpty ())
            {
                logger.info ("No delivered publications found for subscriber : " + subscriptionID);
                return new ArrayList<DeliveryElement> ();
            }
            else
            {
                logger.info (searchedList.size () + " delivered publication(s) found for subscriber : " + subscriptionID);
                return searchedList;
            }
        }
        catch (HibernateException ex)
        {
            logger.error ("Error in searching for delivered publications", ex);
            return new ArrayList<DeliveryElement> ();
        }
    }
    public void deleteDeliveryElements(String subscriptionID)
    {
        ArrayList<DeliveryElement> deliveryElements=getDeliveryElements(subscriptionID);
        Transaction tx = session.beginTransaction ();
        for(DeliveryElement deliveryElement:deliveryElements)
        {
            try
            {
                //Transaction tx = session.beginTransaction ();
                session.delete (deliveryElement);
                //tx.commit ();

            }
            catch (HibernateException ex)
            {
                logger.error ("Error in deleting deliveryElement", ex);

            }
        }
        tx.commit ();
        logger.info("All the delivery elements for the subscriber "+subscriptionID+" are deleted.");

    }
}
